package com.h5lib.server.protocol;

import java.util.HashMap;
import java.util.Map;

/**
 * 协议注册者
 * @author farmer
 *
 */
public class ProtocolRegister {
	/**
	 * 注册者单例
	 */
	private static ProtocolRegister register = null;
	
	private Map<String, ProtocolProvider> map = new HashMap<String, ProtocolProvider>();
	
	private ProtocolProvider defaultProtocolProvider = null;
	
	private ProtocolRegister(){
		
	}
	
	public static ProtocolRegister getProtocolRegister(){
		if(register == null){
			register = new ProtocolRegister();
		}
		return register;
	}
	
	/**
	 * 通过协议获取供应者
	 * @param protocol
	 * @return
	 */
	public ProtocolProvider getProvider(String protocol){
		return map.get(protocol);
	}
	/**
	 * 添加供应者
	 * @param provider
	 */
	public void addProtocolProvider(ProtocolProvider provider){
		addProtocolProvider(provider, true);
	}
	/**
	 * 添加供应者
	 * @param provider
	 * 	
	 * @param override
	 * 	是否重写，默认true
	 */
	public void addProtocolProvider(ProtocolProvider provider , boolean override){
		if(override){
			map.put(provider.getProtocol(), provider);
		}
		else{
			if(map.get(provider.getProtocol()) != null){
				//不处理
			}
			else{
				map.put(provider.getProtocol(), provider);
			}
		}
	}
	/**
	 * 
	 * @return
	 */
	public ProtocolProvider getDefaultProtocolProvider() {
		return defaultProtocolProvider;
	}
	/**
	 * 
	 * @param defaultProtocolProvider
	 */
	public void setDefaultProtocolProvider(ProtocolProvider defaultProtocolProvider) {
		this.defaultProtocolProvider = defaultProtocolProvider;
		addProtocolProvider(defaultProtocolProvider);
	}
	

	
	
}
